
let Cesium:any=undefined;
/**
 * 动态线材质
 * @private
 * @param {string} options.image - 图片url
 * @param {number} options.duration - 持续时长
 * @param {Color} options.color - 颜色
 * @param {JulianDate} options.time - Julian时间
 * @returns {PolylineCustomMaterialProperty}
 */
export class PolylineCustomMaterialProperty extends Cesium.Property{
  private    _definitionChanged:any;
   private _color:any;
   private _colorSubscription:any;
   private _duration:any;
   private _time:any;
   public image:any;
   public MaterialType:any;
    constructor(options:any) {
        super();
        options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
        this._definitionChanged=new Cesium.Event();
        this._color=options.color || Cesium.Color.BLUE;
        this._duration=options.duration || 1000;
        this.image=options.image;
        this.MaterialType= options.MaterialType || "wallType" +new Date().getTime() ;
    }
    get isvarant(){
        return false;
    }
    get definitionChanged(){
        return this._definitionChanged;
    }
    get color(){
        return this._color;
    }
    set color(value){
        this._color=value;
    }
    getType(){
        return this.MaterialType;//暂定
    }
    getValue(time:any, result:any){
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
        result.image = this.image;
        if (this._time === undefined) {
            this._time = time.secondsOfDay;
        }
        result.time = ((time.secondsOfDay - this._time) * 1000) / this._duration;
        return result;
    }

    equals(other:any){
        return (
            this === other || //
            (other instanceof PolylineCustomMaterialProperty && Cesium.Property.equals(this._color, other._color))
        );
    }

}

// 动态线
function   _getDynamicLineShader(options:any) {

    if (options && options.get) {
        return "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
              {\n\
                  czm_material material = czm_getDefaultMaterial(materialInput);\n\
                  vec2 st = materialInput.st;\n\
                  \n\
                  if(texture(image, vec2(0.0, 0.0)).a == 1.0){\n\
                      discard;\n\
                  }else{\n\
                      material.alpha = texture(image, vec2(1.0 - fract(time - st.s), st.t)).a * color.a;\n\
                  }\n\
                  \n\
                  material.diffuse = max(color.rgb * material.alpha * 3.0, color.rgb);\n\
                  \n\
                  return material;\n\
              }\n\
              ";
    }
}

const _initPolylineCustomMaterialProperty=(options:any,CesiumSources:any)=>{
    Cesium=CesiumSources;
    Cesium.Material._materialCache.addMaterial(options.MaterialType, {
        fabric: {
            type: options.MaterialType,
            uniforms: {
                color: options.color || new Cesium.Color(1.0, 0.0, 0.0, 0.5),
                image: options.image,
                time: options.time || 0,
            },
            source: _getDynamicLineShader({
                get: true,
            }),
        },
        translucent: function () {
            return true;
        },
    });
    return new PolylineCustomMaterialProperty(options);
}

export { _initPolylineCustomMaterialProperty }
